/*******************************************************************************
 * 程序说明
 * 
 ******************************************************************************/
#include "../lib/inc/stm8s.h"
#include "../lib/inc/stm8s_clk.h"
#include "../lib/inc/stm8s_gpio.h"

/*******************************************************
 * 休眠1ms
 *******************************************************/
void DelayMS(u16 count)
{
	int j;
	while (count--)
	{
		j = 150;
		while (j--);
	}
}

/*******************************************************
 * 休眠当1时,20um;10时,48um;100时,315um
 *******************************************************/
void DelayUS(u8 count)
{
	while (count--);
}

/*******************************************************
 * 时钟初始化
 *******************************************************/
void CLK_Initialize()
{
	CLK_HSICmd(ENABLE);
	CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8);
}

/*******************************************************
 * GPIO引脚初始化
 *******************************************************/
void GPIO_Initialize()
{
	GPIO_Init(GPIOD, GPIO_PIN_0, GPIO_MODE_OUT_PP_HIGH_SLOW);
}

#define Common GPIOE
#define DP GPIOB

#define DP_Common_B GPIO_PIN_6
#define DP_Common_A GPIO_PIN_7

u8 _digCode[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

void ViewA(u8 num)
{
	GPIO_WriteLow(Common, DP_Common_A);
	GPIO_Write(GPIOB, _digCode[num]);
	DelayMS(6);
	GPIO_WriteHigh(Common, DP_Common_A);
}

void ViewB(u8 num)
{
	GPIO_WriteLow(Common, DP_Common_B);
	GPIO_Write(GPIOB, _digCode[num]);
	DelayMS(6);
	GPIO_WriteHigh(Common, DP_Common_B);
}

void main(void)
{
	CLK_Initialize();
	GPIO_Initialize();

	for (int i = 0; i < 12; i++)
	{
		GPIO_WriteReverse(GPIOD, GPIO_PIN_0);
		DelayMS(80);
	}

	/* Infinite loop */
	while (1)
	{
		GPIO_Init(Common, DP_Common_A, GPIO_MODE_OUT_PP_HIGH_SLOW);
		GPIO_Init(Common, DP_Common_B, GPIO_MODE_OUT_PP_HIGH_SLOW);

		GPIO_Init(DP, GPIO_PIN_0, GPIO_MODE_OUT_PP_HIGH_SLOW);
		GPIO_Init(DP, GPIO_PIN_1, GPIO_MODE_OUT_PP_HIGH_SLOW);
		GPIO_Init(DP, GPIO_PIN_2, GPIO_MODE_OUT_PP_HIGH_SLOW);
		GPIO_Init(DP, GPIO_PIN_3, GPIO_MODE_OUT_PP_HIGH_SLOW);
		GPIO_Init(DP, GPIO_PIN_4, GPIO_MODE_OUT_PP_HIGH_SLOW);
		GPIO_Init(DP, GPIO_PIN_5, GPIO_MODE_OUT_PP_HIGH_SLOW);
		GPIO_Init(DP, GPIO_PIN_6, GPIO_MODE_OUT_PP_HIGH_SLOW);
		GPIO_Init(DP, GPIO_PIN_7, GPIO_MODE_OUT_PP_HIGH_SLOW);

		/* Infinite loop */
		while (1)
		{
			for (u8 i = 0; i <= 99; i++)
			{
				for (u8 j = 0; j < 30; j++)
				{
					u8 g;
					if (i >= 10)
					{
						ViewA(i / 10);
						g = i - (i / 10) * 10;
					}
					else
					{
						ViewA(0);
						g = i;
					}
					ViewB(g);
				}
			}
		}
	}
}

#ifdef USE_FULL_ASSERT

void assert_failed(u8* file, u32 line)
{
	while (1)
	{
	}
}
#endif

/************************ (C) COPYRIGHT xknife.net *************END OF FILE****/

